package com.neuedu.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.neuedu.model.Admin;
import com.neuedu.service.AdminService;
import com.neuedu.util.StringUtil;

@Controller
@RequestMapping(value = "/admin/Admin")
public class AdminAction {

	@Autowired
	private AdminService adminService;

	// *****************************************
	// 登录
	@RequestMapping(value = "/login")
	public void login(Admin admin, HttpServletRequest request, HttpServletResponse response) throws Exception {

		Admin ret = adminService.findAdminByName(admin);
		if (ret != null) {
			// 登录成功，记录用户的session会话中，并跳转dao管理首页
			request.getSession().setAttribute("LOGIN_USER", ret);
			response.sendRedirect(request.getContextPath() + "/admin/index.jsp");
		} else {
			// 登录失败，返回登录页面
			response.sendRedirect(request.getContextPath() + "/admin/login.jsp");
		}
	}

	// 查询所有
	@RequestMapping(value = "/findAll")
	public void findAll( Admin admin ,HttpServletRequest request, HttpServletResponse response) throws Exception {

		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));

		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> pageMap = new HashMap<String, Object>();
		
		pageMap.put("adminName", StringUtil.formatString(admin.getAdminName()));
		pageMap.put("startPage", (page - 1) * rows); // 从第几条数据开始查询
		pageMap.put("endPage", rows); // 每页查询多少条记录

		List<Admin> c = adminService.findAll(pageMap);
		int total = adminService.countAdmin();

		map.put("rows", c);
		map.put("total", total);

		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}

	// 添加
	@RequestMapping(value = "addAdmin")
	public void addCollect(Admin admin, HttpServletRequest request, HttpServletResponse response) throws Exception {
		int res = adminService.addAdmin(admin);
		System.out.println(res);
		Map<String, Object> map = new HashMap<String, Object>();
		if (res > 0) {
			map.put("success", true);
		} else {
			map.put("success", false);
			map.put("errorMsg", "Save user fail !!!");
		}
		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);

	}

	// 修改
	@RequestMapping(value = "updateAdmin")
	public void updateFood(Admin admin, HttpServletRequest request, HttpServletResponse response) throws Exception {

		int res = adminService.updateAdmin(admin);
		Map<String, Object> map = new HashMap<String, Object>();
		if (res > 0) {
			map.put("success", true);
		} else {
			map.put("success", false);
			map.put("errorMsg", "update user fail !!!");
		}
		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}

	// 删除
	@RequestMapping(value = "deleteAdmin")
	public void deleteCollect(Integer adminNo, HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		int res = adminService.deleteAdmin(adminNo);
		Map<String, Object> map = new HashMap<String, Object>();

		if (res > 0) {
			map.put("success", true);
		} else {
			map.put("success", false);
			map.put("errorMsg", "update user fail !!!");
		}

		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}
	
	
	@RequestMapping("/logout")
	public void logout(HttpServletRequest request,HttpServletResponse response) throws Exception {
		//1.使会话失效
		request.getSession().invalidate();
		//2.使会话失效后，返回登录界面
		response.sendRedirect(request.getContextPath()+"/admin/login.jsp");
		
	}
	
}
